#################################################################################
#
# Copyright (c) 2006 Michigan State University
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#################################################################################

#################################################################################
#
#	Author:
#		Nathan Collins <npcollins@gmail.com>
#
#################################################################################

"""
Remaps field names.
"""

import re

# maps alias to actual db fields
alias2actual = {
	'title':'mTitle',
	'author':'mCreator',
	'creator':'mCreator',
	'subject':'mSubject',
	'description':'mDescription',
	'desc':'mDescription',
	'rep':'iRepository',
	'repository':'iRepository',
	'lang':'mLanguage',
	'language':'mLanguage',
	'langcode':'cLanguage',
	'll':'cLanguage',
	'pub':'mPublisher',
	'publisher':'mPublisher',
	'lu':'cValidUrl',
	'type':'mType',
	'format':'mFormat',
	'relation':'mRelation',
	'contrib':'mContributor',
	'contributor':'mContributor',
	'rec':'recHash',
	'hash':'recHash'
	}

# maps actual db fields to casual alias
actual2alias = {
	'mTitle':'title',
	'mCreator':'creator',
	'mSubject':'subject',
	'mDescription':'desc',
	'iRepository':'rep',
	'mLanguage':'lang',
	'cLanguage':'langcode',
	'mPublisher':'pub',
	'cValidUrl':'lu',
	'mType':'type',
	'mFormat':'format',
	'mRelation':'relation',
	'mContributor':'contrib',
	'recHash':'rec'
	}

# maps actual db fields to dc.fields
actual2dc = {
	'mTitle':'dc.title',
	'mSubject':'dc.subject',
	'mSource':'dc.source',
	'mRelation':'dc.relation',
	'mType':'dc.type',
	'mDescription':'dc.description',
	'mCoverage':'dc.coverage',
	'mContributor':'dc.contributor',
	'mCreator':'dc.creator',
	'mPublisher':'dc.publisher',
	'mRights':'dc.rights',
	'mDate':'dc.date',
	'mFormat':'dc.format',
	'mIdentifier':'dc.identifier',
	'mLanguage':'dc.language'
	}

# map dc.fields. to actual db
dc2actual = {
	'dc.title':'mTitle',
	'dc.subject':'mSubject',
	'dc.source':'mSource',
	'dc.relation':'mRelation',
	'dc.type':'mType',
	'dc.description':'mDescription',
	'dc.coverage':'mCoverage',
	'dc.contributor':'mContributor',
	'dc.creator':'mCreator',
	'dc.publisher':'mPublisher',
	'dc.rights':'mRights',
	'dc.date':'mDate',
	'dc.format':'mFormat',
	'dc.identifier':'mIdentifier',
	'dc.language':'mLanguage'
	}


def querymapalias2actual(lucenequery):
	"""
	Given a lucene query, replace any alias label where field is use
	to use the actual db name.
	"""
	# loop through all possible alias
	for alias in alias2actual:
		# if found, replace
		lucenequery = re.sub(r'\b%s:' % alias,'%s:' % alias2actual[alias], lucenequery)

	# return the new query
	return lucenequery


def querymapdc2actual(lucenequery):
	"""
	Given a lucene query, replace any field label where dc.field is use
	to use the actual db name.
	"""
	# loop through all possible dc.fields
	for dc in dc2actual:
		# if found, replace
		lucenequery = re.sub(r'\b%s:' % dc,'%s:' % dc2actual[dc], lucenequery)

	# return the new query
	return lucenequery


def recordmapactual2dc(record):
	"""
	Given a record pulled from the database, map the field names to dc.elements.
	Remove additional fields and return the dc elements as a dict.
	"""
	# dc dict
	dc = {}

	# loop through elements
	for field, value in record.iteritems():
		# check field
		if actual2dc.has_key(field):
			# if matched and replaced, add to dc
			dc[actual2dc[field]] = value
	
	# return the dc dict
	return dc


# eof

